package servlet;

import model.Category;
import service.CategoryService;
import service.GoodsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(name = "CategoryListServlet",urlPatterns = "/admin/category_list")
public class CategoryListServlet extends HttpServlet {
    private CategoryService cService= new CategoryService();
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> list= cService.GetAllType(); //list存储从服务层获取的类别数据

        System.out.println("Category count:" + (list == null ? 0 : list.size()));//测试数据库

        request.setAttribute("list", list);//将数据存入request作用域
        this.getServletContext().removeAttribute("categoryList");
        this.getServletContext().setAttribute("categoryList",list); //把整个Web范围内所有名为categoryList的值都改为list
        request.getRequestDispatcher("/admin/category_list.jsp").forward(request, response);//转发到goods_publish.jsp
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }


}

